//content.js
var utils = require('../../common/util');
var book = 'old';
var windowWidth;
var windowHeight;
var canTurnPage = true;
var lastScrollTop = 0;
var app = getApp();

Page({
  data: {
    chapterIndex: 0,
    chapterCount: 1,
    sectionIndex: 0,
    sectionCount: 1,
    contentScrollTop: 0,
    hideMenu: true,
    loadingHidden: true,
    bookOver: false,
    guideVisible: false
  },

  bookmark: function(book, chapterIndex, sectionIndex){
    wx.setStorageSync('bookmark', {
      book: book,
      chapter: chapterIndex,
      section: sectionIndex
    });
  },

  load: function (book, chapterIndex, sectionIndex, callback){
    var key = book + '_' + chapterIndex + '_' + sectionIndex;
    var url = 'https://v.share-h5.com/bible-server/book.php?book=' + book + '&chapter=' + chapterIndex + '&section=' + sectionIndex;

    app.requestData(key, url, callback);
  },

  loadContent: function(book, chapterIndex, sectionIndex, scroll){
    var self = this;

    this.setData({
      loadingHidden: false
    });

    canTurnPage = false;
    this.load(book, chapterIndex, sectionIndex, function(data){

      var chapterOrder = data.chapter.order;
      var chapterTitle = data.chapter.title;

      var bookOver = data.chapter.index == data.chapter.count - 1 && data.section.index == data.section.count - 1;

      self.setData({
        chapterOrder: chapterOrder,
        chapterTitle: chapterTitle,
        sectionOrder: data.section.order,
        sections: data.section.content,
        chapterIndex: data.chapter.index,
        chapterCount: data.chapter.count,
        sectionIndex: data.section.index,
        sectionCount: data.section.count,
        contentScrollTop: 0,
        bookOver: bookOver
      });

      canTurnPage = true;

      utils.setNavigationBarTitle(chapterOrder + ': ' + chapterTitle);

      self.setData({
        loadingHidden: true,
        contentScrollTop: scroll
      });

      // hack 触发重绘, menu显示不全
      setTimeout(function(){
        self.setData({
          hideMenu: false
        });
      }, 300);

      self.saveScrollPos(scroll);

    });

    this.bookmark(book, chapterIndex, sectionIndex);

  },

  isEdge: function(){
    var ci = this.data.chapterIndex;
    var cc = this.data.chapterCount;
    var si = this.data.sectionIndex;
    var sc = this.data.sectionCount;

    if(ci == 0 && si == 0){
      return 'first';
    }

    if(ci == cc - 1 && si == sc - 1){
      return 'last';
    }

    return false;
  },

  prev: function(){
    if(canTurnPage === false){
      return;
    }

    if(this.isEdge() === 'first'){
      return;
    }

    var ci = this.data.chapterIndex;
    var si = this.data.sectionIndex;

    var toCi;
    var toSi;
    if(si == 0){
      toCi = ci - 1;
      toSi = 10000; // hack到最后一个
    }else{
      toCi = ci;
      toSi = si - 1;
    }

    this.loadContent(book, toCi, toSi);

  },

  next: function(){
    if(canTurnPage === false){
      return;
    }

    if(this.isEdge() === 'last'){
      return;
    }

    var ci = this.data.chapterIndex;
    var si = this.data.sectionIndex;
    var sc = this.data.sectionCount;

    var toCi;
    var toSi;
    if(si == sc - 1){
      toCi = ci + 1;
      toSi = 0;
    }else{
      toCi = ci;
      toSi = si + 1;
    }

    this.loadContent(book, toCi, toSi);    
  },

  getWindowInfo: function(cb){
    if(windowWidth){
      cb && cb();
    }else{
      wx.getSystemInfo({
        success: function(res) {
          windowWidth = res.windowWidth;
          windowHeight = res.windowHeight;
          cb && cb();
        }
      });      
    }
  },

  tapTurn: function(evt){
    var self = this;
    var x = evt.touches[0].clientX;

    this.getWindowInfo(function(){
      var r = x / windowWidth;
      if(r < 0.35){
        self.prev();
      }else if(r > 0.65){
        self.next();
      }else{
        self.setData({
          hideMenu: false
        });
      }
    });
  },

  catalog: function(){
    wx.redirectTo({
      url: '../catalog/catalog?book=' + book + '&chapter=' + this.data.chapterIndex + '&section=' + this.data.sectionIndex
    });    
  },

  onLoad: function (params) {
    book = (params && params.book) || book;
    var chap = (params && params.chapter) || this.data.chapterIndex;
    var sec = (params && params.section) || this.data.sectionIndex;

    var scroll = Number(params.scroll || 0);
    this.loadContent(book, chap, sec, scroll);

    this.showGuide();
  },

  toggleMenu: function(flag){
      this.setData({
        hideMenu: flag
      });
  },

  saveScrollPos: function(top){
    wx.setStorage({
      key: 'content-scroll',
      data: top
    });
  },

  onScroll: function(evt){
    var thredhold = 3;
    if(evt.detail.scrollTop - lastScrollTop > thredhold){
      // 往下滚动
      this.toggleMenu(true);
    }else if(evt.detail.scrollTop - lastScrollTop < -thredhold){
      // 往上滚动     
      this.toggleMenu(false);
    }

    lastScrollTop = evt.detail.scrollTop;
    this.saveScrollPos(lastScrollTop);
  },

  scrollToBottom: function(){
    this.toggleMenu(false);
  },

  hideGuide: function(){
    this.setData({
      guideVisible: false
    });
    wx.setStorageSync('guide', 1);
  },

  showGuide: function(){
    var guide = Number(wx.getStorageSync('guide'));
    if(!guide || guide != 1){
      this.setData({
        guideVisible: true
      });
    }
  },

  aboutUs: function(evt){
    var y = evt.touches[0].clientY;

    this.getWindowInfo(function(){
      if(y / windowHeight < 0.25){
        wx.navigateTo({
          url: '../about/about'
        });
      }
    });
  }
})
